//聊天频道枚举
export const chat_type_enum = {
    当前: [1, "dq"],
    队伍: [2, "dw"],
    世界: [3, "sj"],
    门派: [4, "mp"],
    传闻: [5, "cw"],
    帮派: [6, "bp"],
    传音: [7, "cy"],
};
/**
 * 发送公告
 * @param {*} text 
 */
export function send_notice(text) {
    _global_.send_all_client_message(JSON.stringify({
        "message_type": 59,
        "message_data": text
    }));
}


let id_tx = {
    "绿色ID": [
        "#ls1",
        "#ls2",
        "#ls3",
        "#ls4",
        "#ls5",
        "#ls6",
        "#ls7",
        "#ls8",
        "#ls9"
    ],
    "蓝色ID": [
        "#ns1",
        "#ns2",
        "#ns3",
        "#ns4",
        "#ns5",
        "#ns6",
        "#ns7",
        "#ns8",
        "#ns9"
    ],
    "黄金ID": [
        "#js1",
        "#js2",
        "#js3",
        "#js4",
        "#js5",
        "#js6",
        "#js7",
        "#js8",
        "#js9"
    ],
    "紫色ID": [
        "#zs1",
        "#zs2",
        "#zs3",
        "#zs4",
        "#zs5",
        "#zs6",
        "#zs7",
        "#zs8",
        "#zs9"
    ]
};

function buid_uid_tx(characters) {
    if (characters["id特效"] && id_tx[characters["id特效"]]) {
        let txs = id_tx[characters["id特效"]];
        let result = [];
        result.push("(")
        let id_str = characters["id"] + "";
        for (let index = 0; index < id_str.length; index++) {
            let str = id_str[index];
            result.push(txs[Number(str) - 1]);
        }
        result.push(")");
        return result.join("");
    }
    return "";
}

// 构建超链接文本
export function build_url(characters, urls, text) {
    let url_data = [];
    for (let index = 0; index < urls.length; index++) {
        let url = urls[index];
        switch (url["类型"]) {
            case "道具":
                let item_key = url["点击类型"];
                let item_index = url["编号"] - 1;
                if (characters[item_key] && characters[item_key][item_index]) {
                    let item_data = characters[item_key][item_index];
                    let url_text = G.ChatUtils.build_url_text(item_data, "道具");
                    item_data["索引类型"] = "道具"
                    url_data.push(item_data);
                    text = text.replace(`[${item_data["名称"]}]`, url_text);
                }
                break;
            case "召唤兽":
                let pet_number = url["编号"];
                let pet_data = G.Characters.get_pet_data_by_number(characters, pet_number);
                if (pet_data) {
                    let url_text = G.ChatUtils.build_url_text(pet_data, "召唤兽");
                    text = text.replace(`[${pet_data["名称"]}]`, url_text);
                    pet_data["索引类型"] = "召唤兽"
                    url_data.push(pet_data)
                }
                break;
            default:
                break;
        }
    }
    return [text, url_data];
}

// 构建超链接文本
export function build_url_text(data, type) {
    switch (type) {
        case "玩家信息":
            return `#G/qqq|${data["名称"]}*${data["模型"]}*玩家信息*${data["id"]}/[${data["名称"]}]#W${buid_uid_tx(data)}: `;
        case "召唤兽":
            return `#G/qqq|${data["名称"]}*${data["认证码"]}*召唤兽/[${data["名称"]}]#W`;
        case "道具":
            return `#G/qqq|${data["名称"]}*${data["识别码"]}*道具/[${data["名称"]}]#W`;
        default:
            return `#G/qqq|${data["名称"]}*${data["识别码"]}*${data["类型"]}/[${data["名称"]}]#W`;;
    }
}

/**
 * 发送传音消息
 * @param id 角色id
 * @param characters 角色
 * @param text 消息
 */
export function send_cy_chat(characters, text) {
    _global_.send_all_client_message(JSON.stringify({
        "message_type": 77,
        "message_data": {
            "内容": build_url_text(characters, "玩家信息") + text,
        }
    }));
}

/**
 * 发送传音超链接消息
 * @param id 角色id
 * @param characters 角色
 * @param text 消息
 */
export function send_cy_url_chat(characters, text, url_data) {
    _global_.send_all_client_message(JSON.stringify({
        "message_type": 77,
        "message_data": {
            "内容": build_url_text(characters, "玩家信息") + text,
            "方式": 1,
            "超链接": url_data,
        }
    }));
}

/**
 * 发送聊天消息
 * @param characters 角色名
 * @param text 消息
 * @param pd 频道 
 */
export function send_chat(client_id, characters, text, pd) {
    _global_.send_client_message(client_id, JSON.stringify({
        "message_type": 38,
        "message_data": {
            "内容": build_url_text(characters, "玩家信息") + text,
            "频道": pd,
        }
    }));
}

/**
 * 发送超链接聊天消息
 * @param characters 角色名
 * @param text 消息
 * @param pd 频道 
 */
export function send_url_chat(client_id, characters, text, url_data, pd) {
    _global_.send_client_message(client_id, JSON.stringify({
        "message_type": 38,
        "message_data": {
            "内容": build_url_text(characters, "玩家信息") + text,
            "频道": pd,
            "方式": 1,
            "超链接": url_data,
        }
    }));
}

/**
 * 向所有玩家发送聊天消息
 * @param {*} characters 
 * @param {*} text 
 * @param {*} pd 
 */
export function send_all_chat(characters, text, pd) {
    _global_.send_all_client_message(JSON.stringify({
        "message_type": 38,
        "message_data": {
            "内容": build_url_text(characters, "玩家信息") + text,
            "频道": pd,
        }
    }));
}

/**
 * 向所有玩家发送聊天消息
 * @param {*} text 
 * @param {*} pd 
 */
export function send_all_text_chat(text, pd) {
    _global_.send_all_client_message(JSON.stringify({
        "message_type": 38,
        "message_data": {
            "内容": text,
            "频道": pd,
        }
    }));
}

/**
 * 发送系统聊天消息
 * @param client_id 客户端id 
 * @param text 消息 
 */
export function send_system_chat(client_id, text) {
    _global_.send_client_message(client_id, JSON.stringify({
        "message_type": 38,
        "message_data": {
            "内容": text,
            "频道": "xt",
        }
    }));
}

/**
 * 向角色发送系统聊天消息
 * @param characters_id 角色id
 * @param text 消息 
 */
export function send_character_system_chat(characters_id, text) {
    let client_id = G.Characters.get_characters_client_id(characters_id);
    if (client_id !== undefined) {
        _global_.send_client_message(client_id, JSON.stringify({
            "message_type": 38,
            "message_data": {
                "内容": text,
                "频道": "xt",
            }
        }));
    }
}

/**
 * 发送队伍聊天消息
 * @param client_id 客户端id 
 * @param text 消息 
 */
export function send_team_chat(client_id, text) {
    _global_.send_client_message(client_id, JSON.stringify({
        "message_type": 38,
        "message_data": {
            "内容": text,
            "频道": "dw",
        }
    }));
}

/**
 * 发送帮派消息
 * @param client_id 客户端id 
 * @param text 消息 
 */
export function send_gang_chat(client_id, text) {
    _global_.send_client_message(client_id, JSON.stringify({
        "message_type": 38,
        "message_data": {
            "内容": text,
            "频道": "bp",
        }
    }));
}

/**
 * 向所有人发送系统聊天消息
 * @param text 消息 
 */
export function send_all_sytem_chat(text) {
    _global_.send_all_client_message(JSON.stringify({
        "message_type": 38,
        "message_data": {
            "内容": text,
            "频道": "xt",
        }
    }));
}

/**
 * 向所有人发送超链接消息
 * @param text 消息 
 */
export function send_all_url_chat(characters, text, url_data, pd) {
    _global_.send_all_client_message(JSON.stringify({
        "message_type": 38,
        "message_data": {
            "内容": build_url_text(characters, "玩家信息") + text,
            "频道": pd,
            "方式": 1,
            "超链接": url_data,
        }
    }));
}

/**
 * 向所有人发送系统超链接消息
 * @param text 消息 
 */
export function send_all_sytem_url_chat(text, url_data) {
    _global_.send_all_client_message(JSON.stringify({
        "message_type": 38,
        "message_data": {
            "内容": text,
            "频道": "xt",
            "方式": 1,
            "超链接": url_data,
        }
    }));
}